package com.dingdang.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dingdang.config.AlipayConfig;
import com.dingdang.data.Result;
import com.dingdang.em.ResultEnum;
import com.dingdang.entity.OrderDetails;
import com.dingdang.entity.TbUser;
import com.dingdang.mapper.OrderDetailsMapper;
import com.dingdang.utils.IdWorker;
import com.dingdang.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("order")
public class OrderDetailsController {

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    @Autowired
    private JwtUtil jwtUtil;

    @GetMapping("selectOrder")
    @ResponseBody
    public Result selectInfo(HttpServletRequest request){
        //通过token取出当前登录用户的ID
        String token = request.getHeader("authToken");
        Claims authToken = jwtUtil.parseJwt(token);

        TbUser user = new TbUser();
        LambdaQueryWrapper<OrderDetails> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(OrderDetails::getUid,authToken.getId());
        List<OrderDetails> orderDetails = orderDetailsMapper.selectList(lambdaQuery);
        return Result.response(ResultEnum.SUCCESS).setData(orderDetails);
    }

//    * @param WIDTRout_trade_no 商户订单号，商户网站订单系统中唯一订单号
//     * @param WIDTRtrade_no 支付宝交易号
//     * @param WIDTRrefund_amount 需要退款的金额，该金额不能大于订单金额，必填
//     * @param WIDTRrefund_reason 退款的原因说明
//     * @param WIDTRout_request_no 标识一次退款请求，同一笔交易多次退款需要保证唯一，如需部分退款，则此参数必传
    @GetMapping("refund")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "订单号",required = true,paramType = "query"),
            @ApiImplicitParam(name = "tradeNo",value = "流水号",required = true,paramType = "query"),
            @ApiImplicitParam(name = "price",value = "退款金额",required = true,paramType = "query"),
            @ApiImplicitParam(name = "cause",value = "描述",required = false,paramType = "query"),
            @ApiImplicitParam(name = "sign",value = "标识",required = false,paramType = "query")
    })
    public String refund(@RequestParam String orderId,
                         @RequestParam String tradeNo,
                         @RequestParam String price,
                         @RequestParam(defaultValue = "无")String cause) throws AlipayApiException {
        return alipayTradeRefund(orderId, tradeNo, price, cause,String.valueOf(new IdWorker().nextId()));
    }


    //查询支付功能
    @RequestMapping("alipayTradeQuery")
    @ResponseBody
    public String alipayTradeQuery(String WIDTQout_trade_no, String WIDTQtrade_no ) throws AlipayApiException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();

//        //商户订单号，商户网站订单系统中唯一订单号
//        String out_trade_no = new String(request.getParameter("WIDTQout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//        //支付宝交易号
//        String trade_no = new String(request.getParameter("WIDTQtrade_no").getBytes("ISO-8859-1"),"UTF-8");
        //请二选一设置

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ WIDTQout_trade_no +"\","+"\"trade_no\":\""+ WIDTQtrade_no +"\"}");

        //请求
        String result = alipayClient.execute(alipayRequest).getBody();

        //输出
        //out.println(result);
        return result;
    }
    //退款功能

    /**
     *
     * @param WIDTRout_trade_no 商户订单号，商户网站订单系统中唯一订单号
     * @param WIDTRtrade_no 支付宝交易号
     * @param WIDTRrefund_amount 需要退款的金额，该金额不能大于订单金额，必填
     * @param WIDTRrefund_reason 退款的原因说明
     * @param WIDTRout_request_no 标识一次退款请求，同一笔交易多次退款需要保证唯一，如需部分退款，则此参数必传
     * @return
     * @throws AlipayApiException
     */
    @RequestMapping("alipayTradeRefund")
    public String alipayTradeRefund(String WIDTRout_trade_no,String WIDTRtrade_no,String WIDTRrefund_amount,String WIDTRrefund_reason,String WIDTRout_request_no) throws AlipayApiException {
        System.out.println(WIDTRout_trade_no);
        System.out.println(WIDTRtrade_no);
        System.out.println(WIDTRrefund_amount);
        System.out.println(WIDTRrefund_reason);
        System.out.println(WIDTRout_request_no);
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();

//        //商户订单号，商户网站订单系统中唯一订单号
//        String out_trade_no = new String(request.getParameter("WIDTRout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//        //支付宝交易号
//        String trade_no = new String(request.getParameter("WIDTRtrade_no").getBytes("ISO-8859-1"),"UTF-8");
//        //请二选一设置
//        //需要退款的金额，该金额不能大于订单金额，必填
//        String refund_amount = new String(request.getParameter("WIDTRrefund_amount").getBytes("ISO-8859-1"),"UTF-8");
//        //退款的原因说明
//        String refund_reason = new String(request.getParameter("WIDTRrefund_reason").getBytes("ISO-8859-1"),"UTF-8");
//        //标识一次退款请求，同一笔交易多次退款需要保证唯一，如需部分退款，则此参数必传
//        String out_request_no = new String(request.getParameter("WIDTRout_request_no").getBytes("ISO-8859-1"),"UTF-8");

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ WIDTRout_trade_no +"\","
                + "\"trade_no\":\""+ WIDTRtrade_no +"\","
                + "\"refund_amount\":\""+ WIDTRrefund_amount +"\","
                + "\"refund_reason\":\""+ WIDTRrefund_reason +"\","
                + "\"out_request_no\":\""+ WIDTRout_request_no +"\"}");

        //请求
        String result = alipayClient.execute(alipayRequest).getBody();

        //输出
        //out.println(result);
        System.out.println(result);
        return "refund-success";
    }


}
